package com.example.cloudcommon.config.util;

import com.alibaba.druid.support.jakarta.StatViewServlet;
import com.alibaba.druid.support.jakarta.WebStatFilter;
import jakarta.servlet.Filter; // Spring Boot 3 使用 jakarta
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DruidConfig {

    @Bean
    public ServletRegistrationBean<StatViewServlet> druidServlet() {
        ServletRegistrationBean<StatViewServlet> reg = new ServletRegistrationBean<>();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", "admin"); // 用户名
        reg.addInitParameter("loginPassword", "123456"); // 密码
        reg.addInitParameter("resetEnable", "false"); // 禁用重置
        return reg;
    }

    @Bean
    public FilterRegistrationBean<WebStatFilter> druidFilter() {
        FilterRegistrationBean<WebStatFilter> reg = new FilterRegistrationBean<>();
        reg.setFilter(new WebStatFilter());
        reg.addUrlPatterns("/*");
        reg.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return reg;
    }

//    @Bean
//    public FilterRegistrationBean<Filter> webStatFilter() { // 这里使用 jakarta.servlet.Filter
//        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
//        registrationBean.setFilter(new WebStatFilter()); // 使用支持 Jakarta 的 WebStatFilter
//        registrationBean.addUrlPatterns("/*");
//        registrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
//
//        return registrationBean;
//    }
}